package com.htgd.gardenexpo.utils;

import cn.hutool.core.util.StrUtil;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.StorageClass;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.reflect.Field;

/**
 * @author ：hejing
 * @date ：Created in 2021/6/12 15:17
 * @description：天翼云
 * @modified By：`
 * @version: 1.0
 */
@Component
public class GetAmazonS3Client {
    @Value("${spring.file-storage.aws-s3[0].end-point}")
    public String endPoint;

    @Value("${spring.file-storage.aws-s3[0].access-key}")
    public String accessKey;

    @Value("${spring.file-storage.aws-s3[0].secret-key}")
    public String secretKey;

    @Value("${spring.file-storage.aws-s3[0].region}")
    public String region;
    @Value("${spring.file-storage.aws-s3[0].bucket-name}")
    public String bucket_name;

    public AmazonS3 getClient() {
        AmazonS3ClientBuilder builder = (AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(this.accessKey, this.secretKey)));
        if (StrUtil.isNotBlank(endPoint)) {
            builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(this.endPoint, this.region));
        } else if (StrUtil.isNotBlank(region)) {
            builder.withRegion(region);
        }

        return (AmazonS3)builder.build();

       /* ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setConnectionTimeout(30*1000);     //设置连接的超时时间，单位毫秒
        clientConfig.setSocketTimeout(30*1000) ;        //设置socket超时时间，单位毫秒
        clientConfig.setProtocol(Protocol.HTTP);        //设置http


        //负载是否参与签名、设置参与
        S3ClientOptions options = new S3ClientOptions();
        options.setPayloadSigningEnabled(true);

        *//* 创建client*//*
        AmazonS3 ossClient = new AmazonS3Client(
                new PropertiesCredentials(this.accessKey, this.secretKey),clientConfig);

        // 设置endpoint
        ossClient.setEndpoint("oos-sccd.ctyunapi.cn");

        //设置选项
        ossClient.setS3ClientOptions(options);
        return ossClient;*/
    }

    public  PutObjectResult putObject(String key, File file){
        AmazonS3 amazonS3=getClient();

        System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "false");
        PutObjectResult result ;
        PutObjectRequest request = new PutObjectRequest(bucket_name,key,file);
        request.setStorageClass(StorageClass.Standard);
        result = amazonS3.putObject(request);
//        System.out.println("put object:" + key + ",md5: "+ result.getContentMd5());
        return result;
    }

    public void delete(String path){
        AmazonS3 amazonS3=getClient();
        try {
            amazonS3.deleteObject(bucket_name,path);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
